package routers

import (
	"bee-web/controllers"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"net/http"
	"path"
	"strings"
)

func init() {
	beego.InsertFilter("/", beego.BeforeRouter, WebServerFilter)
	beego.InsertFilter("/*", beego.BeforeRouter, WebServerFilter)

	beego.Router("/api/login/account", &controllers.BaseController{}, "post:Test")
}

//静态文件后缀
const staticFileExt = ".html|.js|.css|.png|.jpg|.jpeg|.ico|.otf"

func WebServerFilter(ctx *context.Context) {
	urlPath := ctx.Request.URL.Path
	if urlPath == "" || urlPath == "/" {
		urlPath = "index.html"
	}
	
	ext := path.Ext(urlPath)
	index := strings.Index(staticFileExt, ext)
	if index >= 0 {
		http.ServeFile(ctx.ResponseWriter, ctx.Request, "static/"+urlPath)
	}
}
